package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.PaymentRecordQueryResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 15:08
 */
public class PaymentRecordQueryBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(PaymentRecordQueryBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String billingCycleId = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Billing_Cycle");
        msgBody.put("billingCycleId", billingCycleId);
        String objValue = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id");
        String objAttr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/DestinationAttr");

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);

        Map<String, String> svcObjectStruct = new HashMap<>();
        svcObjectStruct.put("objValue", objValue);
        svcObjectStruct.put("objAttr", objAttr);
        svcObjectStruct.put("objType", "3");
        svcObjectStruct.put("dataArea", "0");
        msgBody.put("svcObjectStruct", svcObjectStruct);

        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

      		LOG.info("request body is {}", msgBodyStr.toString());
            LOG.info("webserviceUrl is {}", url);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
            LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        PaymentRecordQueryResponse paymentRecordQueryResponse = new Gson().fromJson(respStr, PaymentRecordQueryResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", paymentRecordQueryResponse.getResultCode());
        respDataXml.put("Para_Field_Result", paymentRecordQueryResponse.getResultMsg());

        List<PaymentRecordQueryResponse.PaymentInfo> paymentInfoList = paymentRecordQueryResponse.getPaymentInfoList();
        String bodyItem = "";
        if (paymentInfoList != null) {
            for (PaymentRecordQueryResponse.PaymentInfo query : paymentInfoList) {
                SortedMap<Object, Object> balanceInfoXml = new TreeMap<>();
                balanceInfoXml.put("Req_Serial", query.getPaymentId());
                balanceInfoXml.put("Payment_Amount", query.getAmount());
                balanceInfoXml.put("Payment_Method", query.getPaymentMethod());
                balanceInfoXml.put("PayChannelId", query.getPaymentChannel());
                balanceInfoXml.put("StartTime", query.getPaymentDate());
                balanceInfoXml.put("AccNbrDetail", query.getAccNbrDetail());
                balanceInfoXml.put("Latn_Id", "");
                balanceInfoXml.put("BalanceTypeFlag", query.getBalanceTypeId());
                respDataXml.put("Payment_Record_info", balanceInfoXml);
                try {
                    bodyItem+="<Payment_Record_info>"+HttpClientUtils.mapToSignXml(balanceInfoXml)+"</Payment_Record_info>";
                } catch (Exception e) {
                    bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                    LOG.error("Stitching XML error", e);
                    return bizResult;
                }
            }
        }
        String respXmlStr = "";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respDataXmlStr += bodyItem;
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
